/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.controllers;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.StatusLineContributionItem;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.PlatformUI;
import org.toedo.controllers.events.EventController;
import org.toedo.logging.Log;
import org.toedo.utils.Constants;
import org.toedo.Activator;

public class Controller implements PropertyChangeListener {
	
	/**
	 * Holder for singleton implementation.
	 */
	private static class ControllerHolder {
		private static final Controller INSTANCE = new Controller();
	}
	
	/**
	 * Get the unique instance of the Controller.
	 * @return The instance of the Controller
	 */
	public static Controller getInstance() {
		return ControllerHolder.INSTANCE;
	}
	
	private IPreferenceStore mPreferenceStore;
	private IStatusLineManager mStatuslineManager;
	private StatusLineContributionItem mPerspectiveStatusLine;
	
	private boolean mApplicationStopping = false;
	
	private Controller() {
		mPreferenceStore = Activator.getDefault().getPreferenceStore();
		
		EventController.getInstance().addGeneralEventsListener(this);
		
		Log.getInstance(Controller.class).info("Controller initialized."); //$NON-NLS-1$
	}
	
	/**
	 * Get the preference store.
	 * @return The preference store.
	 */
	public IPreferenceStore getPreferenceStore() {
		return mPreferenceStore;
	}
	
	/**
	 * Tell if the application is currently stopping.
	 * @return True if the application is stopping
	 */
	public boolean isAplicationStopping() {
		return mApplicationStopping;
	}
	
	/**
	 * Set the application stopping flag.
	 * @param value
	 */
	public void setApplicationStopping(boolean value) {
		mApplicationStopping = value;
	}
	
	public void setStatusLineManager(IStatusLineManager value) {
		mStatuslineManager = value;
	}
	
	public void setStatusLineMessage(String message) {
		mStatuslineManager.setMessage(message);
	}
	
	public void initializePerspectiveStatusLine(IStatusLineManager statusLine) {
		mPerspectiveStatusLine = new StatusLineContributionItem("ProjectId"); //$NON-NLS-1$
		statusLine.add(mPerspectiveStatusLine);		
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		
		String projectName = DataController.getInstance().getProject().getProjectName();
		if (DataController.getInstance().getProject().isModified()) {
			projectName = "*" + projectName; //$NON-NLS-1$
		}
		
		mPerspectiveStatusLine.setText(projectName);
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setText(String.format("%s - %s", Constants.APP_NAME, projectName)); //$NON-NLS-1$
		/*
		if ((evt.getPropertyName().equals(EventConstants.EVT_OPEN)) ||
				(evt.getPropertyName().equals(EventConstants.EVT_PROJECT_PROPERTIES_CHANGE))) {
			mPerspectiveStatusLine.setText(DataController.getInstance().getProject().getProjectName());
			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setText(String.format("%s - %s", Constants.APP_NAME, DataController.getInstance().getProject().getProjectName())); //$NON-NLS-1$
		}
		*/
	}
	
	public void dispose() {
		EventController.getInstance().removeGeneralEventsListener(this);
	}

}
